﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BLL.Sys;
using Model.Sys;
using Common;
using Enums.Sys;
using Enums;
using BLL.Organization;
namespace DAGG.Controllers.sys
{
    public class SysMenuController : BaseController
    {
        MenuBLL bll = new  MenuBLL();
        Sys_RoleBLL roleBll = new Sys_RoleBLL();
        EnterpriseBLL bll0 = new EnterpriseBLL();
        //
        // GET: /SysMenu/
        public ActionResult Index()
        {
            SysUserModel userModel = base.GetUserInfo();

        
            string strWhere = string.Empty;
            //递归获取其所属子公司和子公司的子公司
            if (EnumManagement.GetEnumValue(SysRoleEnum.SuperManageRole) == userModel.RoleId)//管理员角色 查看全部
                strWhere += "  ";
            else
            {
                var listWhere = string.Join(",", bll0.GetSonEnterprise(userModel.EnterpriseId).Select(obj => obj.ID));
                if (string.IsNullOrEmpty(listWhere))
                    strWhere += " and (EnterpriseId  in (0))";
                else
                    strWhere += " and (EnterpriseId in (" + listWhere + "))";
            }
            //绑定角色下拉框
            List<Sys_RoleModel> roleList = roleBll.Getlist(strWhere);
            List<SelectListItem> selectRodeListItem = new List<SelectListItem>();

            foreach (var item in roleList)
            {
                
                selectRodeListItem.Add(new SelectListItem { Text = item.RoleName, Value = item.ID.ToString(), Selected = false });
            }
            ViewData["selectRoleList"] = selectRodeListItem;
            return View();
        }
        //一级菜单
        public ActionResult GetParentMenu()
        {
            SysUserModel userModel = base.GetUserInfo();
            //登录用户角色ID 
            //根据角色ID得到菜单权限Id
            string MenuIds = bll.GetMenuByRoleId(userModel.RoleId.ToString());
            //根据菜单权限ID得到具体菜单信息
            List<MenuModel> list = bll.GetParentMenuByIds(MenuIds);
            return Json(list, JsonRequestBehavior.AllowGet);
        }
        //二级菜单
    [HttpPost]
        public ActionResult GetSubMenu(string menuId)
        {
            SysUserModel userModel = base.GetUserInfo();
            string MenuIds = bll.GetMenuByRoleId(userModel.RoleId.ToString());
            List<Model.Sys.MenuModel> list = bll.GetSubMenuByParentId(menuId,MenuIds);
            return Json(list, JsonRequestBehavior.AllowGet);
          
        }

     [HttpPost]
    public ActionResult SaveData(string roleId,string menuId) {
         //授权操作
        Sys_RoleBLL roleBll = new Sys_RoleBLL();
        Sys_RoleModel roleModel = new Sys_RoleModel
        { 
            ID=PageValidate.ConvertToTrimInt(roleId),
            MenuId=menuId
        };

        ResultEntity entity = roleBll.UpMenu(roleModel);
        return Json(entity);
        
    }

        [HttpPost]

     public ActionResult GetMenuForRole() {
         MenuBLL menuBll = new MenuBLL();
         List<ztreemodel> menuList = menuBll.GetAllMenu();
         return Json(menuList);
        
     }


	}
}